<?php
/**
 * Minhlog Domain_Model 文章分类类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Domain
 * @package    Domain_Model
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Category.php 2 2010-01-17 13:49:08Z Minh.Shu $
 *
 */

class Domain_Model_Category
{
    private $_categoryDataMapper;
    private $_categoryEntity;

    public function  __construct()
    {
        $this->_categoryDataMapper = new Domain_DataMapper_Category();
        $this->_categoryEntity = new Domain_Entity_Category();
        $this->_categoryDataMapper->setEntity($this->_categoryEntity);
    }

    /**
     * 根据分类ID查询数据
     * @param integer $categoryId
     * @return object|void
     */
    public function getCategoryById($categoryId)
    {
        $categoryId = (int) $categoryId;
        return $this->_categoryDataMapper->findByPrimaryId($categoryId);
    }

    /**
     * 查询所有分类数据
     * @param null|integer $parentId
     * @return object|void
     */
    public function getAllCategory($parentId = null)
    {
        return $this->_categoryDataMapper->findAllCategory($parentId);
    }

    /**
     * 根据分类根节点ID查询分类数据
     * @return object|void
     */
    public function getCategoryTree()
    {
        $parentCategorySet = $this->_categoryDataMapper->findRootCategoryTree();
        $childCategorySet = $this->_categoryDataMapper->findChildCategoryTree($parentCategorySet);
        $resultSet = array();

        if($parentCategorySet->count()){
            foreach ($parentCategorySet as $parentCategory) {
                $parent = $this->_categoryDataMapper->mapper($parentCategory);
                $categoryTree = new stdClass();
                $categoryTree->parent = $parent;
                if(array_key_exists($parentCategory->category_id, $childCategorySet)){
                    $categoryTree->childs = $childCategorySet[$parentCategory->category_id];
                }
                $resultSet[] = $categoryTree;
            }
        }else{
            $resultSet = null;
        }

        return $resultSet;
    }

    /**
     * 根据父类查询相关子类
     * @param integer $categoryId
     * @return object|void
     */
    public function getRelatedCategory($categoryId)
    {
        $categoryId = (int) $categoryId;
        return $this->_categoryDataMapper->findChildCategoryByParent($categoryId);
    }

    /**
     * 保存分类信息
     * @param array $data
     * @param integer $primaryId
     * @return
     */
    public function saveCategory($data, $primaryId = 0)
    {
        $primaryId = (int) $primaryId;
        return $this->_categoryDataMapper->save($data, $primaryId);
    }

    /**
     * 删除分类信息
     * @param integer $primaryId
     * @return
     */
    public function delCategory($primaryId)
    {
        $primaryId = (int) $primaryId;
        return $this->_categoryDataMapper->deleteDependent($primaryId);
    }
}